package com.dcloud.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.flow.entity.AipComp;
import com.dcloud.flow.entity.AipCompClass;
import com.dcloud.flow.mapper.AipCompClassMapper;
import com.dcloud.flow.mapper.AipCompMapper;
import com.dcloud.flow.mapper.xml.NodeVO;
import com.dcloud.flow.service.IAipCompService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.LinkedList;
import java.util.List;

/**
 * @Description: aip_comp
 * @Author: jeecg-boot
 * @Date:   2023-07-25
 * @Version: V1.0
 */
@Service
public class AipCompServiceImpl extends ServiceImpl<AipCompMapper, AipComp> implements IAipCompService {

    @Autowired
    AipCompClassMapper aipCompClassMapper;

    @Autowired
    AipCompMapper aipCompMapper;

    @Override
    public List<NodeVO> tree() {
        List<AipCompClass> aipCompClasses = aipCompClassMapper.selectList(new LambdaQueryWrapper<>());
        List<NodeVO> groups = new LinkedList<>();
        aipCompClasses.forEach(e -> {
            NodeVO nodeVO = new NodeVO();
            nodeVO.setId(e.getId().toString());
            nodeVO.setOpen(true);
            nodeVO.setName(e.getClassName());
            nodeVO.setIco("el-icon-video-pause");
            nodeVO.setType("group");

            List<NodeVO> children = new LinkedList<>();
            List<AipComp> aipComps = aipCompMapper.selectList(new LambdaQueryWrapper<AipComp>().eq(AipComp::getClassId, e.getId()).eq(AipComp::getIsDelete, 0));
            aipComps.forEach(child -> {
                NodeVO nodeChild = new NodeVO();
                nodeChild.setId(child.getId().toString());
                nodeChild.setName(child.getCompName());
                nodeChild.setCode(child.getCompCode());
                nodeChild.setIco(child.getIcon());
                nodeChild.setType(child.getCompCode());
                nodeChild.setConfigJson(child.getCompParamJson());
                children.add(nodeChild);
            });

            nodeVO.setChildren(children);
            groups.add(nodeVO);

        });

        return groups;
    }

}
